package cn.gcjxsc.controller.merchant;

import cn.gcjxsc.consts.CheckStatus;
import cn.gcjxsc.consts.ErrorConst;
import cn.gcjxsc.data.query.salesman.ApplyInfoQuery;
import cn.gcjxsc.data.query.salesman.SalesmanQuery;
import cn.gcjxsc.data.vo.salesman.ApplyInfoVo;
import cn.gcjxsc.data.vo.salesman.SalesmanVo;
import cn.gcjxsc.entity.salesman.ApplyInfo;
import cn.gcjxsc.entity.salesman.Salesman;
import cn.gcjxsc.service.integral.ApplyInfoService;
import cn.gcjxsc.service.integral.SalesmanService;
import com.zgxl.common.util.AjaxResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 商户操作类.
 *
 * @author pumbaa
 * @since 1.0
 */
@RestController("merchantSalesmanController")
@RequestMapping("/merchant")
public class SalesmanController extends MerchantBaseController {

    @Autowired
    private SalesmanService salesmanService;
    @Autowired
    private ApplyInfoService applyInfoService;


    /**
     * 查询业务员申请列表
     *
     * @param filter   过滤条件
     * @param page     当前页
     * @param pageSize 每页记录数
     * @return
     */
    @RequestMapping("/applyInfoList")
    public AjaxResponse queryApplyInfoList(ApplyInfoQuery filter,
                                        @RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                        @RequestParam(value = "pageSize", defaultValue = "30", required = false) Integer pageSize) {
        page = Math.abs(page);
        Page<ApplyInfo> info = applyInfoService.queryApplyInfo(filter, page, pageSize);
        List<ApplyInfo> applyInfos = info.getContent();
        long count = info.getTotalElements();
        List<ApplyInfoVo> voList = applyInfoService.converList(applyInfos);
        Map<String, Object> data = new HashMap<>();
        data.put("count", count);
        data.put("list", voList);
        return AjaxResponse.ajaxSuccess(data);
    }

    /**
     * 商户审核业务申请表.
     *
     * @param id 申请id
     */
    @RequestMapping(value = "/checkApplyInfo", method = RequestMethod.POST)
    public AjaxResponse checkApplyInfo(@RequestParam("id") Integer id,
                                    @RequestParam("status") Integer status,
                                    @RequestParam(value = "remark", defaultValue = "", required = false) String remark) {
        Assert.notNull(id, "申请记录不能为空");
        ApplyInfo applyInfo = salesmanService.findApplyInfoById(id);
        if (null == applyInfo) {
            return AjaxResponse.ajaxFailure(ErrorConst.RECORD_NOT_EXIST);
        } else if (applyInfo.getStatus() != CheckStatus.WAIT) {
            return AjaxResponse.ajaxFailure("当前状态不允许该操作");
        }
        CheckStatus checkStatus = CheckStatus.fromCode(status);
        if (checkStatus == CheckStatus.WAIT) {
            return AjaxResponse.ajaxFailure("审核状态不正确");
        }
        try {
            salesmanService.checkApplyInfo(applyInfo, "", checkStatus, remark == null ? "" : remark);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResponse.ajaxSuccess();
    }

    /**
     * 查询业务员列表
     *
     * @param filter   过滤条件
     * @param page     当前页
     * @param pageSize 最大记录数
     * @return
     */
    @RequestMapping("/salesman/list")
    public AjaxResponse querySalesmanList(SalesmanQuery filter,
                                       @RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                       @RequestParam(value = "pageSize", defaultValue = "30", required = false) Integer pageSize) {
        page = Math.abs(page);
        Page<Salesman> queryList = salesmanService.queryList(filter, page, pageSize);
        long count = queryList.getTotalElements();
        List<Salesman> salesmanList = queryList.getContent();
        List<SalesmanVo> voList = salesmanService.convertList(salesmanList);

        Map<String, Object> data = new HashMap<>();
        data.put("count", count);
        data.put("list", voList);
        return AjaxResponse.ajaxSuccess(data);
    }

    /**
     * 解绑店铺业务员
     *
     * @param id      业务员id
     * @param storeId 店铺id
     * @return
     */
    @RequestMapping("/salesman/unbind")
    public AjaxResponse unbindSalesmanAction(@RequestParam("id") Integer id, @RequestParam("storeId") Integer storeId) {
        Salesman salesman = salesmanService.findById(id);
        if (null == salesman) {
            return AjaxResponse.ajaxFailure(ErrorConst.RECORD_NOT_EXIST);
        }
        if (!Objects.equals(salesman.getStoreId(), storeId)) {
            return AjaxResponse.ajaxFailure(ErrorConst.ILLEGAL_OPERATION);
        }
        salesman.setStatus(0);
        try {
            salesmanService.unbindSalesman(salesman);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResponse.ajaxSuccess();
    }

    /**
     * 更新业务收益比例
     *
     * @return
     */
    @RequestMapping(value = "/salesman/updateRate")
    public AjaxResponse updateSalesmanRateAction(@RequestParam("id") Integer salesmanId,
                                              @RequestParam("ratio") BigDecimal ratio) {
        Salesman salesman = salesmanService.findById(salesmanId);
        if (null == salesman) {
            return AjaxResponse.ajaxFailure("数据不存在");
        }
        if (ratio.compareTo(BigDecimal.ZERO) < 0) {
            ratio = BigDecimal.ZERO;
        }
        try {
            salesmanService.updateSalesmanRatio(salesman, ratio);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResponse.ajaxSuccess();
    }
}
